namespace DCHealthCheck.DomainModel {

    #region Usings
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Diagnostics;
    using System.Text; 
    #endregion

    [Table("InstalledProduct")]
    public class InstalledProduct : IInstalledProduct, IObjectWithState, IValidatableObject {

        #region Members
        #region Max length fields
        public static readonly int DescriptionMaxLength = 900;
        public static readonly int NameMaxLength = 900;
        public static readonly int VendorMaxLength = 900;
        public static readonly int VersionMaxLength = 255;
        #endregion

        public virtual string Description { get; set; }

        public virtual long Id { get; set; }

        public virtual Guid IdentifyingNumber { get; set; }

        public virtual DateTime InstallDate { get; set; }

        public virtual string Name { get; set; }

        public virtual string Vendor { get; set; }

        public virtual string Version { get; set; }

        #region Related Entities
        [ForeignKey("SystemInfo")]
        public virtual Guid SystemInfoId {
            get {
                if (this.SystemInfo != null) return this.SystemInfo.Id;
                return systemInfoId;
            }
            set { systemInfoId = value; }
        }
        private Guid systemInfoId;
        public virtual SystemInfo SystemInfo {
            get { return systemInfo; }
            set {
                systemInfo = value;
                if (systemInfo != null) {
                    if (this.SystemInfoId != systemInfo.Id) {
                        this.SystemInfoId = systemInfo.Id;
                    }
                }
            }
        }
        private SystemInfo systemInfo;
        #endregion 

        #region NotMapped properties
        [NotMapped]
        public DetachedState DetachedState { get; set; }

        [NotMapped]
        public Object EntityKey { get { return this.Id; } }

        [NotMapped]
        public Dictionary<string, object> StartingOriginalValues { get; set; } 
        #endregion
        #endregion

        #region Constructor
        public InstalledProduct() {
            this.Description = string.Empty;
            this.Name = string.Empty;
            this.Vendor = string.Empty;
            this.Version = string.Empty;
        } 
        #endregion

        #region Methods
        [DebuggerStepThroughAttribute]
        public override string ToString() {
            var info = new StringBuilder();

            info.Append("InstalledProduct:  ");
            if ((this.SystemInfo != null) && (this.SystemInfo.DomainController != null) && (this.SystemInfo.DomainController.SamAccountNameFriendly != null)) {
                info.AppendFormat("DC: {0}; ", this.SystemInfo.DomainController.DNSName);
            }
            if (this.SystemInfo != null) {
                info.AppendFormat("HealthCheck Date: {0}; ", this.SystemInfo.CheckDateTimeUTC.ToString("yyyy-MMM-dd"));
            }
            info.AppendFormat("Product Name: {0}", this.Name != null ? this.Name : "NULL");

            return info.ToString();
        }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
            if ((this.DetachedState != DetachedState.Added) && (this.Id < 1)) {
                yield return new ValidationResult(
                    "Id is required.", new[] { "Id" });
            }

            if (this.DetachedState != DetachedState.Deleted) {
                // add entity to SystemInfo, let DetectChanges perform relationship fixup
                if (this.SystemInfoId == Guid.Empty) {
                    yield return new ValidationResult(
                        "SystemInfoId is required.", new[] { "SystemInfoId" });
                }

                if ((this.SystemInfo != null) && (this.SystemInfo.Id != this.SystemInfoId)) {
                    yield return new ValidationResult(
                        "SystemInfo.Id does not match SystemInfoId.", new[] { "SystemInfo, SystemInfoId" });
                }

                if (this.Description == null) {
                    yield return new ValidationResult(
                        "Description is required.", new[] { "Description" });
                }
                else if (this.Description.Length > DescriptionMaxLength) {
                    yield return new ValidationResult(string.Format("Description length: {0} exceeds max length: {1}",
                        this.Description.Length, DescriptionMaxLength), new[] { "Description" });
                }

                if (string.IsNullOrWhiteSpace(this.Name)) {
                    yield return new ValidationResult(
                        "Name is required.", new[] { "Name" });
                }
                else if (this.Name.Length > NameMaxLength) {
                    yield return new ValidationResult(string.Format("Name length: {0} exceeds max length: {1}",
                        this.Name.Length, NameMaxLength), new[] { "Name" });
                }

                if (this.Vendor == null) {
                    yield return new ValidationResult(
                        "Vendor is required.", new[] { "Vendor" });
                }
                else if (this.Vendor.Length > VendorMaxLength) {
                    yield return new ValidationResult(string.Format("Vendor length: {0} exceeds max length: {1}",
                        this.Vendor.Length, VendorMaxLength), new[] { "Vendor" });
                }

                if (string.IsNullOrWhiteSpace(this.Version)) {
                    yield return new ValidationResult(
                        "Version is required.", new[] { "Version" });
                }
                else if (this.Version.Length > VersionMaxLength) {
                    yield return new ValidationResult(string.Format("Version length: {0} exceeds max length: {1}",
                        this.Version.Length, VersionMaxLength), new[] { "Version" });
                }
            } // if (this.DetachedState != State.Deleted)
        }  
        #endregion

    }
}
